package com.justcan.demo.cartoon.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.Transformation;

import com.justcan.demo.cartoon.R;
import com.justcan.demo.cartoon.utils.anim.touch.TouchAnimObject;

/**
 * 火箭
 *
 * @author zhanghuijun
 */
public class RocketObject extends TouchAnimObject {

    /**
     * 宽高
     */
    private float mWidth = 0;
    private float mHeight = 0;
    /**
     * 坐标
     */
    private int mX = 0;
    private int mY = 0;
    /**
     * 是否已经初始化
     */
    private boolean mHasInit = false;
    /**
     * 图
     */
    private Bitmap[] mRocketBmp = null;
    /**
     * 当前哪张图
     */
    private int mBitmapIndex = 0;
    /**
     * 绘制Rect
     */
    private Rect mSrcRect = null;
    private Rect mDstRect = null;
    /**
     * Paint
     */
    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
    /**
     * 是否正在被点击中
     */
    private boolean mIsInTouch = false;
    /**
     * 点击处的距离
     */
    private int mTouchDisX = 0;
    private int mTouchDisY = 0;
    /**
     * 上一次动画时间
     */
    private long mLastLogicAnimTime = 0l;
    /**
     * 动画阶段
     * 第一阶段，准备发射
     * 第二阶段，起飞
     */
    private int mAnimStage = 1;
    /**
     * 起飞动画
     */
    private RocketTransAnimation mLaunchAnimation = null;

    public RocketObject(View mRootAnimView, Context mContext) {
        super(mRootAnimView, mContext);
        if (getAnimSceneWidth() != 0 && getAnimSceneHeight() != 0) {
            init();
        }
    }

    @Override
    public void onSizeChange(int w, int h) {
        super.onSizeChange(w, h);
        init();
    }

    /**
     * 初始化
     */
    private void init() {
        if (!mHasInit) {
            mRocketBmp = new Bitmap[]{
                    BitmapFactory.decodeResource(getContext().getResources(), R.drawable.rocket_1),
                    BitmapFactory.decodeResource(getContext().getResources(), R.drawable.rocket_2),
            };
            int mSceneWidth = getAnimSceneWidth();
            int mSceneHeight = getAnimSceneHeight();
            mWidth = mSceneWidth * 0.2f;    // 宽度是场景宽度的0.2
            mHeight = mWidth * 2.41f;        // 根据图片计算合理的高度
            mX = (int) ((mSceneWidth - mWidth) / 2);
            mY = mSceneHeight * 1 / 6;
            mSrcRect = new Rect(0, 0, mRocketBmp[mBitmapIndex].getWidth(), mRocketBmp[mBitmapIndex].getHeight());
            mDstRect = new Rect(mX, mY, (int) (mWidth + mX), (int) (mHeight + mY));
            mHasInit = true;
        }
    }

    @Override
    public void logic(long animTime, long deltaTime) {
        if (mLastLogicAnimTime == 0l) {
            mLastLogicAnimTime = animTime;
        }
        // 每一定时间换一次图，产生动的效果
        long now = System.currentTimeMillis();
        if (now - mLastLogicAnimTime > 100) {
            mBitmapIndex++;
            mBitmapIndex = mBitmapIndex % mRocketBmp.length;
            mLastLogicAnimTime = now;
        }
        if (mAnimStage == RocketAnimGroup.ANIM_STAGE_LAUNCH) {
            // 处于第二阶段，起飞
            if (mLaunchAnimation == null) {
                mLaunchAnimation = new RocketTransAnimation();
                // 直接从当前位置开始起飞
                mLaunchAnimation.mStartX = mX;
                mLaunchAnimation.mStartY = mY;
                // 下面两行是让火箭从正中间开始起飞
//				mLaunchAnimation.mStartX = (int) ((getAnimSceneWidth() - mWidth) / 2);
//				mLaunchAnimation.mStartY = (int) (getAnimSceneHeight() - mHeight);
                mLaunchAnimation.mEndX = mX;
                mLaunchAnimation.mEndY = (int) (-2 * mHeight);
                mLaunchAnimation.setDuration(1000);
                mLaunchAnimation.setInterpolator(new AccelerateInterpolator());        // 加速插值
            }
            mLaunchAnimation.getTransformation(animTime, null);
            mDstRect.set(mX, mY, (int) (mWidth + mX), (int) (mHeight + mY));
        }
    }

    @Override
    public void draw(Canvas canvas, int sceneWidth, int sceneHeight) {
        if (mHasInit) {
            canvas.drawBitmap(mRocketBmp[mBitmapIndex], mSrcRect, mDstRect, mPaint);
        }
    }

    @Override
    public boolean onTouch(MotionEvent event) {
        int action = event.getAction();
        int x = (int) event.getX();
        int y = (int) event.getY();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                if (isTouchIn(x, y)) {
                    mTouchDisX = mX - x;
                    mTouchDisY = mY - y;
                    mIsInTouch = true;
                    return true;
                }
                break;
            case MotionEvent.ACTION_MOVE:
                if (mIsInTouch) {
                    mX = x + mTouchDisX;
                    mY = y + mTouchDisY;
                    mDstRect.set(mX, mY, (int) (mWidth + mX), (int) (mHeight + mY));
                    return true;
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                if (mIsInTouch) {
                    mIsInTouch = false;
                    return true;
                }
                break;

            default:
                break;
        }
        return false;
    }

    /**
     * 是否点击中了
     */
    private boolean isTouchIn(int x, int y) {
        if (x >= mX && x <= mX + mWidth
                && y >= mY && y <= mY + mHeight) {
            return true;
        }
        return false;
    }

    /**
     * 获取火箭位置
     */
    public Rect getRocketRect() {
        return mDstRect;
    }

    /**
     * 设置动画阶段
     */
    public void setAnimStage(int stage) {
        mAnimStage = stage;
    }

    /**
     * 火箭的平移动画
     */
    class RocketTransAnimation extends Animation {

        /**
         * 起始位置
         */
        public int mStartX = 0;
        public int mStartY = 0;
        /**
         * 预期结束位置
         */
        public int mEndX = 0;
        public int mEndY = 0;

        @Override
        protected void applyTransformation(float interpolatedTime,
                                           Transformation t) {
            // 改变真实的位置坐标
            mX = (int) (mStartX + (mEndX - mStartX) * interpolatedTime);
            mY = (int) (mStartY + (mEndY - mStartY) * interpolatedTime);
        }
    }
}
